package com.mspbots.rule.exceptions.ticket;

import cn.hutool.core.util.StrUtil;
import com.mspbots.cw.request.TenantTicketDTO;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @ClassName com.mspbots.rule.exceptions.ticket.TicketInProcess
 * @Description TODO
 * @Author Jun
 * @Date 2019/8/13 17:35
 * @Version 1.0
 **/
@Slf4j
@Rule(name = "TicketInProgress", description = "")
public class TicketInProgress extends TicketBaseRule {

    protected StringRedisTemplate stringRedisTemplate;

    @Override
    @Condition
    public boolean condition(@Fact(FACT) TenantTicketDTO ticket) {
        try {
//            log.info("TicketInProgress {} {}", ticket.getCwId(), ticket.getStatus());
            if (!ticket.getStatus().toLowerCase().contains("in progress")
                    && !ticket.getStatus().toLowerCase().contains("in-progress")) {
                return false;
            }

            if (!setUserMapping(ticket.getTenantUserId())) {
                return false;
            }
            messages.setBusinessId(ticket.getCwId());
            messages.setBusinessType(BUSINESS_TYPE);
            if (checkMessage(ticket)) {
                return false;
            }
            if (this.tenantRuleDTO.getRuleParams().get("Time").asText().toLowerCase().contains("m")) {
                return ticket.getUpdatedTime().plusMinutes(Integer.parseInt(this.tenantRuleDTO.getRuleParams().get("Time").asText().replace("m", ""))).isBefore(LocalDateTime.now());

            } else if (this.tenantRuleDTO.getRuleParams().get("Time").asText().toLowerCase().contains("h")) {
                return ticket.getUpdatedTime().plusHours(Integer.parseInt(this.tenantRuleDTO.getRuleParams().get("Time").asText().replace("h", ""))).isBefore(LocalDateTime.now());
            }
            return false;
        } catch (Exception e) {
            log.error("TicketInProgress : {}", e.getMessage());
        }
        return false;
    }

    @Action
    @Async
    @Override
    public void action(@Fact(FACT) TenantTicketDTO tenantTicketDTO) {
//        String message = buildMessage(tenantTicketDTO.getCreatedTime());
        String message = "";
        if (this.tenantRuleDTO.getRuleParams().get("Time").asText().toLowerCase().contains("m")) {
            message = buildMessage(tenantTicketDTO.getUpdatedTime().plusMinutes(Integer.parseInt(this.tenantRuleDTO.getRuleParams().get("Time").asText().replace("m", ""))));
        } else if (this.tenantRuleDTO.getRuleParams().get("Time").asText().toLowerCase().contains("h")) {
            message = buildMessage(tenantTicketDTO.getUpdatedTime().plusHours(Integer.parseInt(this.tenantRuleDTO.getRuleParams().get("Time").asText().replace("h", ""))));
        }
        Map<String, Object> params = Map.of("TicketLink", suffixTicketLink(tenantTicketDTO.getCwId()));
        message = StrUtil.format(message, params);
        messages.setMessage(message);
        messages.setBusinessId(tenantTicketDTO.getCwId());
        insertMessage();
    }

}
